/************************************************************************************/
/*  Program name CAPAPEXT_AP_OPT.sql                                                */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
set verify off;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAP_AP_OPT';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPAP_AP_OPT.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_SOB_SName	VARCHAR2(100);
	lv_SOB_Count	NUMBER;

	CURSOR Cur_F(p_NGO_SOB VARCHAR2) IS
	SELECT
		F.*
	FROM
		GL_SETS_OF_BOOKS			B,
		AP_SYSTEM_PARAMETERS_ALL	F
	WHERE	UPPER(B.Short_Name)	=	p_NGO_SOB
	AND		B.Set_Of_Books_ID	=	F.Set_Of_Books_ID;

	lv_Create_By	VARCHAR2(100);
	lv_Update_By	VARCHAR2(100);

	lv_Terms		VARCHAR2(50);
	lv_AP_Terms		VARCHAR2(50);
	lv_Org_Name		VARCHAR2(240);

BEGIN
	lv_SOB_SName	:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_SOB_Count
	FROM	GL_SETS_OF_BOOKS
	WHERE	UPPER(Short_Name)	=	lv_SOB_SName;

	IF	lv_SOB_Count	=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_SOB_SName) LOOP

			IF	C1.Org_ID	IS	NOT NULL	THEN
				SELECT	Name
				INTO	lv_Org_Name
				FROM
					HR_All_Organization_Units
				WHERE	Organization_ID	=	C1.Org_ID;
			ELSE
				lv_Org_Name	:=	NULL;
			END IF;

			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
					C1.Set_Of_Books_ID||CHR(9)||
				C1.Base_Currency_Code||CHR(9)||
				C1.Recalc_Pay_Schedule_Flag||CHR(9)||
				C1.Auto_Calculate_Interest_Flag||CHR(9)||
				C1.Invoice_Currency_Code||CHR(9)||
				C1.Payment_Currency_Code||CHR(9)||
				C1.Invoice_Net_Gross_Flag||CHR(9)||
				C1.Bank_Account_ID||CHR(9)||
				C1.Check_Overflow_Lookup_Code||CHR(9)||
				C1.Batch_Control_Flag||CHR(9)||
					C1.Terms_ID||CHR(9)||
				C1.Always_Take_Disc_Flag||CHR(9)||
				C1.Pay_Date_Basis_Lookup_Code||CHR(9)||
					C1.Accts_Pay_Code_Combination_ID||CHR(9)||
					C1.Sales_Tax_Code_Combination_ID||CHR(9)||
					C1.Disc_Lost_Code_Combination_ID||CHR(9)||
					C1.Disc_Taken_Code_Combination_ID||CHR(9)||
					C1.Hold_Gain_Code_Combination_ID||CHR(9)||
					C1.Trans_Gain_Code_Combination_ID||CHR(9)||
				C1.Apply_Advances_Default||CHR(9)||
				C1.Add_Days_Settlement_Date||CHR(9)||
				C1.Cost_Of_Money||CHR(9)||
				C1.Days_Between_Check_Cycles||CHR(9)||
				C1.Federal_Identification_Num||CHR(9)||
					C1.Location_ID||CHR(9)||
				C1.Create_Employee_Vendor_Flag||CHR(9)||
				C1.Employee_Terms_ID||CHR(9)||
				C1.Employee_Pay_Group_Lookup_Code||CHR(9)||
				C1.Employee_Payment_Priority||CHR(9)||
					C1.Prepay_Code_Combination_ID||CHR(9)||
				C1.Confirm_Date_As_Inv_Num_Flag||CHR(9)||
				C1.Update_Pay_Site_Flag||CHR(9)||
				C1.Default_Exchange_Rate_Type||CHR(9)||
					C1.Gain_Code_Combination_ID||CHR(9)||
					C1.Loss_Code_Combination_ID||CHR(9)||
				C1.Make_Rate_Mandatory_Flag||CHR(9)||
				C1.Multi_Currency_Flag||CHR(9)||
				C1.GL_Date_From_Receipt_Flag||CHR(9)||
				C1.Disc_Is_Inv_Less_Tax_Flag||CHR(9)||
				C1.Match_On_Tax_Flag||CHR(9)||
				C1.Accounting_Method_Option||CHR(9)||
				C1.Expense_Post_Option||CHR(9)||
				C1.Discount_Taken_Post_Option||CHR(9)||
				C1.Gain_Loss_Post_Option||CHR(9)||
				C1.Cash_Post_Option||CHR(9)||
				C1.Future_Pay_Post_Option||CHR(9)||
				C1.Date_Format_Lookup_Code||CHR(9)||
				C1.Replace_Check_Flag||CHR(9)||
				C1.Online_Print_Flag||CHR(9)||
				C1.Eft_User_Number||CHR(9)||
				C1.Max_Outlay||CHR(9)||
				C1.Vendor_Pay_Group_Lookup_Code||CHR(9)||
				C1.Require_Tax_Entry_Flag||CHR(9)||
				C1.Approvals_Option||CHR(9)||
				C1.Post_Dated_Payments_Flag||CHR(9)||
				C1.Secondary_Accounting_Method||CHR(9)||
				C1.Secondary_Set_Of_Books_ID||CHR(9)||
				C1.Take_Vat_Before_Discount_Flag||CHR(9)||
				C1.Interest_Tolerance_Amount||CHR(9)||
					C1.Interest_Code_Combination_ID||CHR(9)||
				C1.Terms_Date_Basis||CHR(9)||
				C1.Allow_Future_Pay_Flag||CHR(9)||
				C1.Auto_Tax_Calc_Flag||CHR(9)||
				C1.Automatic_Offsets_Flag||CHR(9)||
				C1.Liability_Post_Lookup_Code||CHR(9)||
					C1.Interest_Accts_Pay_CCID||CHR(9)||
				C1.Liability_Post_Option||CHR(9)||
				C1.Discount_Distribution_Method||CHR(9)||
					C1.Rate_Var_Code_Combination_ID||CHR(9)||
				C1.Combined_Filing_Flag||CHR(9)||
				C1.Income_Tax_Region||CHR(9)||
				C1.Income_Tax_Region_Flag||CHR(9)||
				C1.Hold_Unmatched_Invoices_Flag||CHR(9)||
				C1.Allow_Dist_Match_Flag||CHR(9)||
				C1.Allow_Final_Match_Flag||CHR(9)||
				C1.Allow_Flex_Override_Flag||CHR(9)||
				C1.Allow_Paid_Invoice_Adjust||CHR(9)||
				C1.Ussgl_Transaction_Code||CHR(9)||
				C1.Ussgl_Trx_Code_Context||CHR(9)||
				C1.Inv_Doc_Category_Override||CHR(9)||
				C1.Pay_Doc_Category_Override||CHR(9)||
				C1.Vendor_Auto_Int_Default||CHR(9)||
				C1.Summary_Journals_Default||CHR(9)||
					C1.Rate_Var_Gain_CCID||CHR(9)||
					C1.Rate_Var_Loss_CCID||CHR(9)||
				C1.Transfer_Desc_Flex_Flag||CHR(9)||
				C1.Allow_Awt_Flag||CHR(9)||
				C1.Default_Awt_Group_ID||CHR(9)||
				C1.Allow_Awt_Override||CHR(9)||
				C1.Create_Awt_Dists_Type||CHR(9)||
				C1.Create_Awt_Invoices_Type||CHR(9)||
				C1.AWT_Include_Discount_Amt||CHR(9)||
				C1.AWT_Include_Tax_Amt||CHR(9)||
					C1.Org_ID||CHR(9)||
				C1.Recon_Accounting_Flag||CHR(9)||
				C1.Auto_Create_Freight_Flag||CHR(9)||
					C1.Freight_Code_Combination_ID||CHR(9)||
				C1.Global_Attribute_Category||CHR(9)||
				C1.Global_Attribute1||CHR(9)||
				C1.Global_Attribute2||CHR(9)||
				C1.Global_Attribute3||CHR(9)||
				C1.Global_Attribute4||CHR(9)||
				C1.Global_Attribute5||CHR(9)||
				C1.Global_Attribute6||CHR(9)||
				C1.Global_Attribute7||CHR(9)||
				C1.Global_Attribute8||CHR(9)||
				C1.Global_Attribute9||CHR(9)||
				C1.Global_Attribute10||CHR(9)||
				C1.Global_Attribute11||CHR(9)||
				C1.Global_Attribute12||CHR(9)||
				C1.Global_Attribute13||CHR(9)||
				C1.Global_Attribute14||CHR(9)||
				C1.Global_Attribute15||CHR(9)||
				C1.Global_Attribute16||CHR(9)||
				C1.Global_Attribute17||CHR(9)||
				C1.Global_Attribute18||CHR(9)||
				C1.Global_Attribute19||CHR(9)||
				C1.Global_Attribute20||CHR(9)||
				C1.Allow_Supplier_Bank_Override||CHR(9)||
				C1.Use_Multiple_Supplier_Banks||CHR(9)||
				C1.Auto_Tax_Calc_Override||CHR(9)||
				C1.Amount_Includes_Tax_Flag||CHR(9)||
				C1.Amount_Includes_Tax_Override||CHR(9)||
				C1.VAT_Code||CHR(9)||
				C1.Use_Bank_Charge_Flag||CHR(9)||
				C1.BANK_CHARGE_BEARER||CHR(9)||
					C1.Rounding_Error_CCID||CHR(9)||
				C1.Rounding_Error_Post_Option||CHR(9)||
				C1.Tax_From_Po_Flag||CHR(9)||
				C1.Tax_From_Vendor_Site_Flag||CHR(9)||
				C1.Tax_From_Vendor_Flag||CHR(9)||
				C1.Tax_From_Account_Flag||CHR(9)||
				C1.Tax_From_System_Flag||CHR(9)||
				C1.Tax_From_Inv_Header_Flag||CHR(9)||
				C1.Tax_From_Template_Flag||CHR(9)||
				C1.Tax_Hier_PO_Shipment||CHR(9)||
				C1.Tax_Hier_Vendor||CHR(9)||
				C1.Tax_Hier_Vendor_Site||CHR(9)||
				C1.Tax_Hier_Account||CHR(9)||
				C1.Tax_Hier_System||CHR(9)||
				C1.Tax_Hier_Invoice||CHR(9)||
				C1.Tax_Hier_Template||CHR(9)||
				C1.Enforce_Tax_From_Account||CHR(9)||
				C1.Mrc_Base_Currency_Code||CHR(9)||
				C1.Mrc_Secondary_Set_Of_Books_ID||CHR(9)||
				C1.Match_Option||CHR(9)||
				C1.Gain_Loss_Calc_Level||CHR(9)||
				C1.When_To_Account_Pmt||CHR(9)||
				C1.When_To_Account_Gain_Loss||CHR(9)||
				C1.Future_Dated_Pmt_Acct_Source||CHR(9)||
				C1.Future_Dated_Pmt_Liab_Relief||CHR(9)||
				C1.GL_Transfer_Allow_Override||CHR(9)||
				C1.GL_Transfer_Process_Days||CHR(9)||
				C1.GL_Transfer_Mode||CHR(9)||
				C1.GL_Transfer_Submit_Journal_Imp||CHR(9)||
				C1.Include_Reporting_Sob||CHR(9)||
				C1.Expense_Report_ID||CHR(9)||
					C1.Prepayment_Terms_ID||CHR(9)||
				C1.Calc_User_Xrate||CHR(9)||
				C1.Sort_By_Alternate_Field||CHR(9)||
				C1.Approval_Workflow_Flag||CHR(9)||
				C1.Allow_Force_Approval_Flag||CHR(9)||
				C1.Validate_Before_Approval_Flag||CHR(9)||
				C1.Xml_Payments_Auto_Confirm_Flag||CHR(9)||
				C1.Prorate_Int_Inv_Across_Dists||CHR(9)||
				C1.Build_Prepayment_Accounts_Flag||CHR(9)||
				C1.Enable_1099_On_Awt_Flag||CHR(9)||
				C1.Stop_Prepay_Across_Bal_Flag||CHR(9)||
				C1.Automatic_Offsets_Change_Flag||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				lv_SOB_SName||CHR(9)||
				CAPBREXT_COMMON.GetAPTerm(C1.Terms_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Accts_Pay_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Sales_Tax_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Disc_Lost_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Disc_Taken_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Hold_Gain_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Trans_Gain_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetLocation(C1.Location_ID)||CHR(9)||
				CAPBREXT_COMMON.GetAPTerm(C1.Employee_Terms_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Prepay_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Gain_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Loss_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Interest_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Interest_Accts_Pay_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Rate_Var_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Rate_Var_Gain_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Rate_Var_Loss_CCID)||CHR(9)||
				lv_Org_Name||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Freight_Code_Combination_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSegment(C1.Rounding_Error_CCID)||CHR(9)||
				CAPBREXT_COMMON.GetAPTerm(C1.Prepayment_Terms_ID)
				);

			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_SOB_SName||': No such set of books exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/